package com.example.infrastructure.framework.security.handler;

import com.alibaba.fastjson2.JSON;
import com.example.common.core.domain.Result;
import com.example.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义退出处理类 返回成功
 *
 * @author ruoyi
 */
@Slf4j
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
{

    /**
     * 退出处理
     *
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException
    {
        // TODO 获取登录用户
        /*LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            String userName = loginUser.getUsername();
            // TODO 记录用户退出日志，删除用户缓存记录
            log.info("用户已登出（{}）",userName);
        }*/
        ServletUtils.renderString(response, JSON.toJSONString(Result.success("退出成功")));
    }
}
